<template>
  <div class="app-container hk-flex-column">
    <search-form
      :data="queryParams"
      :field-list="filterInfo.fieldList"
      :list-type-info="dictList"
      @handleSearch="handleQuery"
      @handleReset="resetQuery" />
    <el-row>
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      <el-button size="mini" icon="el-icon-delete" @click="handleDelete">删除</el-button>
    </el-row>

    <hk-table
      :table-data="tableData"
      :table-columns="tableList"
      :selection="true"
      :show-index="true"
      :total="total"
      :loading="loading"
      :table-height="80"
      operate
      :operate-width="258"
      :query-params="queryParams"
      @handleSelectionChange="handleSelectionChange"
      @changePage="getDataList">
      <!-- 启用 -->
      <template #status="scope">
        <el-switch
          :value="scope.row.status == '0' ? true : false"
          :active-text="scope.row.status == '0' ? '开启' : '关闭'"
          @change="changeStatus($event, scope.row)" />
      </template>
      <!-- 操作权限 -->
      <template #operate="scope">
        <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        <el-button size="mini" type="text" @click="handleRecord(scope.row)">执行记录</el-button>
      </template>
    </hk-table>
    <RecordDrawer ref="record_drawer" />
  </div>
</template>
  
<script>
import { pageList, deleteItem, getStatus } from '@/api/specialProject/periodicity'
import SearchForm from '@/components/HkSearchform/index.vue'
import HkTable from '@/components/Hk-Table/index.vue'
import RecordDrawer from './components/recordDrawer.vue'

export default {
  components: {
    SearchForm,
    HkTable,
    RecordDrawer,
  },
  data() {
    return {
      queryParams: {
        current: 1,
        size: 10,
      },
      checkedList: [],
      ids: [],
      tableData: [],
      total: 0,
      loading: false,
      tableList: [
        { label: '任务名称', param: 'jobName' },
        { label: '任务状态', param: 'status', type: 'slot' },
        { label: '执行周期', param: 'cronType' },
        { label: '上次执行时间', param: 'finalTime' },
        { label: '下次执行时间', param: 'nextTime' },
        { label: '创建人', param: 'createUser' },
        { label: '创建时间', param: 'createTime' },
      ],
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'jobName', placeholder: '请输入任务名称', maxlength: 20 },
          { label: '类型', type: 'select', field: 'cronType', list: 'typeList' },
        ],
      },
      dictList: {
        typeList: [
          {
            label: '周期性',
            value: '0',
          },
          {
            label: '定期',
            value: '1',
          },
        ],
      },
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    handleRecord(row) {
      this.$refs.record_drawer.show(row.id)
    },
    // 修改状态
    changeStatus(e, item) {
      console.log(e, item, 'ssss')
      var obj = {
        id: item.id,
        status: e === true ? 0 : 1,
      }
      getStatus(obj).then((res) => {
        this.getDataList()
      })
    },
    //  查询
    handleQuery() {
      this.queryParams.current = 1
      this.getDataList()
    },
    //  重置
    resetQuery() {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDataList()
    },

    getDataList(val) {
      this.queryParams = { ...this.queryParams, ...val }
      this.loading = true
      pageList(this.queryParams).then((res) => {
        res.list.forEach((element) => {
          for (const key in element) {
            element[key] = element[key] || '—'
          }
        })
        this.loading = false
        this.tableData = res.list
        this.total = res.total
      })
    },
    handleSelectionChange(obj) {
      this.checkedList = obj
      this.ids = obj.map((item) => item.id)
    },
    handleDetail(row) {
      this.title = '详情'
      this.$router.push({ path: '/specialProject/periodicityDetail', query: { id: row.id, title: '详情' } })
    },
    handleAdd() {
      this.title = '新增'
      this.$router.push({ path: '/specialProject/periodicityDetail', query: { title: '新增' } })
    },
    handleEdit(row) {
      this.title = '编辑'
      this.$router.push({ path: '/specialProject/periodicityDetail', query: { id: row.id, title: '修改' } })
    },
    handleSubmit() {},
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function () {
          return deleteItem(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getDataList()
        })
        .catch((error) => {
          console.log(error)
        })
    },
  },
}
</script>
  
    <style lang="scss" scoped>
.el-row {
  position: absolute;
  right: 20px;
  top: 50px;
}
.total-title {
  margin-bottom: 15px;
  margin-top: 6px;
  line-height: 24px;
}
.font-size16 {
  font-size: 16px;
}
.flex-1 {
  flex: 1;
  justify-content: space-between;
  height: 100%;
}
.font-c-blue {
  color: #4b6eef;
  font-size: 36px;
}
.mt-20 {
  margin-top: 20px;
}
::v-deep {
  .content {
    padding-bottom: 0;
  }
}
</style>
  
  